package com.stxx.stiot2.event.task;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.stxx.framwork.constants.AlarmPolicyType;
import com.stxx.stiot2.constants.RedisKeys;
import com.stxx.stiot2.domain.Alarm;
import com.stxx.stiot2.domain.Scene;
import com.stxx.stiot2.domain.SysConfig;
import com.stxx.stiot2.domain.Terminal;
import com.stxx.stiot2.domain.cache.TerminalCache;
import com.stxx.stiot2.domain.ext.AlarmPlan;
import com.stxx.stiot2.domain.ext.AlarmState;
import com.stxx.stiot2.domain.ext.Period;
import com.stxx.stiot2.event.dto.T2pMsg;
import com.stxx.stiot2.service.SysConfigService;
import com.stxx.stiot2.service.TerminalService;
import com.stxx.stiot2.utils.ServerUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.noear.solon.Solon;
import org.noear.solon.expression.snel.SnEL;

import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class SubscribeReportTask implements Runnable{
    private final static Pattern p = Pattern.compile("#\\{(\\w+)\\}");
    private T2pMsg message;
    private final static boolean stop = true;
    public SubscribeReportTask(T2pMsg message) {
        this.message = message;
    }
    @Override
    public void run() {
        //if(stop)return;
        SysConfigService configService = Solon.context().getBean(SysConfigService.class);
        TerminalService terminalService = Solon.context().getBean(TerminalService.class);
        JSONObject jsonObject = configService.getConfig(SysConfigService.Keys.signal_subscribe);
        JSONObject jsonObject1 = configService.getConfig(SysConfigService.Keys.token);
        String token = jsonObject1.getString("value");
        String url = jsonObject.getString("data_up_url");
        if(StrUtil.isEmpty(url)){
            return;
        }
        TerminalCache terminal =terminalService.getCache(message.getTerminalCode());
        if(terminal == null){
            log.info("订阅上报数据未推送，终端不存在");
            return;
        }else if(terminal.getDeviceId()==null){
            log.info("订阅上报数据未推送，终端"+terminal.getCode()+"没有关联设备");
            return;
        }
        DeviceReport deviceReport = BeanUtil.toBean(message, DeviceReport.class);
        deviceReport.setDeviceId(terminal.getDeviceId());
        HttpRequest request = HttpRequest.post(url);
        String timestamp = System.currentTimeMillis()+"";
        request.header("timestamp",timestamp);
        request.header("sign", DigestUtil.md5Hex(token+timestamp));
        request.body(JSON.toJSONString(deviceReport));
        try {
            HttpResponse response = request.execute();
            log.debug("订阅上报数据推送:"+url+"["+response.isOk()+"]:"+response.body());
        }catch (Exception ex){
            log.error(ex.getMessage());
        }
    }

    @Data
    public static class DeviceReport extends T2pMsg{
        private String deviceId;
    }
}
